package com.yalantis.taurus.sample;

import com.yalantis.taurus.library.MyAdapter;
import com.yalantis.taurus.library.PullToRefreshView;
import com.yalantis.taurus.sample.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.ArrayList;

public class MainAbility extends Ability {

    private PullToRefreshView mRecyclerView;
    private MyAdapter<String> mAdapter;
    private ArrayList<String> listData;
    private final EventHandler handler = new EventHandler(EventRunner.getMainEventRunner());
    private boolean isFirst=true;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
//        super.setMainRoute(MainAbilitySlice.class.getName());
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
    }
    private void initView(){
        mRecyclerView = (PullToRefreshView)findComponentById(ResourceTable.Id_abt_linear_xrecyclerview);
        mRecyclerView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                handler.removeTask(refreshRunnable);
                if(isFirst){
                    isFirst=false;
                    handler.postTask(refreshRunnable,200);
                }else {
                    handler.postTask(refreshRunnable, 5000);
                }
            }
        });

        listData = new ArrayList<>();
        mAdapter = new MyAdapter<>(getContext(),listData,ResourceTable.Layout_list_item);

        mAdapter.setOnItemClickListener(new PullToRefreshView.RecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Component component, int position) {

                new ToastDialog(getContext()).setText("nihao"+position).show();
            }
        });
        mRecyclerView.setItemProvider(mAdapter);
        mRecyclerView.refresh();
    }

    private final Runnable refreshRunnable = new Runnable() {
        @Override
        public void run() {
            listData.clear();
            try {
                listData.add(getResourceManager().getElement(ResourceTable.String_trip_to_india).getString());
                listData.add(getResourceManager().getElement(ResourceTable.String_trip_to_italy).getString());
                listData.add(getResourceManager().getElement(ResourceTable.String_trip_to_indonesia).getString());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NotExistException e) {
                e.printStackTrace();
            } catch (WrongTypeException e) {
                e.printStackTrace();
            }
            mAdapter.notifyDataChanged();
            if(mRecyclerView != null)
                mRecyclerView.refreshComplete();
        }
    };
}
